package gui.standard.form;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.tablemodel.RadnikTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JDigitsTextField;
import util.components.JLimitedTextField;
import actions.standard.form.ZoomFormAction;

public class RadnikForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JDigitsTextField tfJMBG;
	private JLimitedTextField tfSifraPreduzeca;
	private JLimitedTextField tfIme;
	private JLimitedTextField tfPrezime;
	private JLimitedTextField tfKorisnickoIme;
	private JLimitedTextField tfLozinka;

	private JButton bPreduzeceZoom;

	public RadnikForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		tfJMBG = new JDigitsTextField(13, 13, false, true);
		tfSifraPreduzeca = new JLimitedTextField(12, 12, true);
		tfIme = new JLimitedTextField(30, 30, true);
		tfPrezime = new JLimitedTextField(30, 30, true);
		tfKorisnickoIme = new JLimitedTextField(30, 30, true);
		tfLozinka = new JLimitedTextField(30, 30, true);
		
		setTitle("Radnik");
		
		tableModel = new RadnikTableModel(new String[] {
				"JMBG", "Sifra preduzeca", "Ime", "Prezime", "Korisnicko ime",
				"Lozinka" }, 0);
		
		tblGrid.setModel(tableModel);
		
		bPreduzeceZoom = new JButton(new ZoomFormAction(this,
				new PreduzeceForm()));
		
		children.add(new ClanPopisneKomisijeForm());
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblJMBG = new JLabel("JMBG:");
		JLabel lblSifraPreduzeca = new JLabel("Sifra preduzeca:");
		JLabel lblIme = new JLabel("Ime:");
		JLabel lblPrezime = new JLabel("Prezime:");
		JLabel lblKorisnickoIme = new JLabel("Korisnicko ime:");
		JLabel lblLozinka = new JLabel("Lozinka:");

		dataPanel.add(lblJMBG);
		dataPanel.add(tfJMBG, "wrap");
		dataPanel.add(lblSifraPreduzeca);
		dataPanel.add(tfSifraPreduzeca);
		dataPanel.add(bPreduzeceZoom, "wrap");
		dataPanel.add(lblIme);
		dataPanel.add(tfIme, "wrap");
		dataPanel.add(lblPrezime);
		dataPanel.add(tfPrezime, "pushx");
		dataPanel.add(lblKorisnickoIme);
		dataPanel.add(tfKorisnickoIme, "wrap");
		dataPanel.add(lblLozinka);
		dataPanel.add(tfLozinka, "pushx");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfJMBG.setText("");
			tfSifraPreduzeca.setText("");
			tfIme.setText("");
			tfPrezime.setText("");
			tfKorisnickoIme.setText("");
			tfLozinka.setText("");
			return;
		}
		String jmbg = (String) tableModel.getValueAt(index, 0);
		String sifraPreduzeca = (String) tableModel.getValueAt(index, 1);
		String ime = (String) tableModel.getValueAt(index, 2);
		String prezime = (String) tableModel.getValueAt(index, 3);
		String korisnickoIme = (String) tableModel.getValueAt(index, 4);
		String lozinka = (String) tableModel.getValueAt(index, 5);

		tfJMBG.setText(jmbg);
		tfSifraPreduzeca.setText(sifraPreduzeca);
		tfIme.setText(ime);
		tfPrezime.setText(prezime);
		tfKorisnickoIme.setText(korisnickoIme);
		tfLozinka.setText(lozinka);

		setMode(StandardForm.MODE_EDIT);
	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String jmbg = tfJMBG.getText().trim();
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String ime = tfIme.getText().trim();
		String prezime = tfPrezime.getText().trim();
		String korisnickoIme = tfKorisnickoIme.getText().trim();
		String lozinka = tfLozinka.getText().trim();
		try {
			int index = tableModel.insertRow(new String[] { jmbg,
					sifraPreduzeca, ime, prezime, korisnickoIme, lozinka });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String jmbg = tfJMBG.getText().trim();
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String ime = tfIme.getText().trim();
		String prezime = tfPrezime.getText().trim();
		String korisnickoIme = tfKorisnickoIme.getText().trim();
		String lozinka = tfLozinka.getText().trim();
		try {
			tableModel.searchRows(new String[] { jmbg, sifraPreduzeca, ime,
					prezime, korisnickoIme, lozinka });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String jmbg = tfJMBG.getText().trim();
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String ime = tfIme.getText().trim();
		String prezime = tfPrezime.getText().trim();
		String korisnickoIme = tfKorisnickoIme.getText().trim();
		String lozinka = tfLozinka.getText().trim();
		try {
			int index = tableModel.editRow(new String[] { jmbg, sifraPreduzeca,
					ime, prezime, korisnickoIme, lozinka },
					tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfJMBG.setText("");
		tfSifraPreduzeca.setText("");
		tfIme.setText("");
		tfPrezime.setText("");
		tfKorisnickoIme.setText("");
		tfLozinka.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfJMBG.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		if(form instanceof PreduzeceForm)
			tfSifraPreduzeca.setText(keys[0]);
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		return new String[]{"", tfJMBG.getText().trim(), "", "", "", "", ""};
	}

}
